<script lang="ts" setup>
import {getCategoryApi, mainRoute} from "../serve"
import {useRouter} from 'vue-router'
import {ref} from "vue"
import pagination from "./pagination.vue"
import {Search} from "@element-plus/icons-vue";

const
    router = useRouter(),
    itemData = ref({list: [], current: 1, size: 10, total: 0}),
    fromData = ref({size: 10, page: 1, categoryId: null, keyword: ''}),
    load = ref(true),
    categories = ref<any[]>([])
;

function get() {
  mainRoute(fromData.value).then((r: any) => {
    itemData.value = r
    load.value = false
    // console.table(r)
  })
}

function sizeChange(size: number) {
  fromData.value.size = size
  get()
}

function currentChange(currPage: number) {
  fromData.value.page = currPage
  get()
}

function getCategory() {
  getCategoryApi({
    page: 1,
    size: 99999,
    type: "ROUTE"
  }).then((r: any) => {
    console.log(r);
    categories.value = r.list;
  })
}

function reset() {
  fromData.value.categoryId = null;
  fromData.value.keyword = ''
  get();
}

get()

function openNewTab(id: any) {
  const r = router.resolve({path: '/routeTon-details', query: {id}})
  window.open(r.href, '_blank')
}
</script>

<template>
  <el-scrollbar class="container">
    <div con style="width: 80%;margin: 0 10%">
      <div v-loading="load" element-loading-text="加载中..." travel>
        <div tr-top>
          <div dis>
            <div>旅游路线</div>
          </div>
        </div>
        <div class="search_box">
          <el-input style="width: 240px" :prefix-icon="Search" v-model="fromData.keyword"
                    maxlength="20" show-word-limit
                    clearable
                    placeholder="请输入路线名称"></el-input>
          <el-select v-model="fromData.categoryId"
                     filterable
                     :remote="true"
                     :remote-method="getCategory"
                     clearable
                     style="width: 240px;margin-left: 20px" placeholder="请选择类别">
            <el-option v-for="item in categories" :key="item.categoryId" :label="item.name" :value="item.categoryId"/>
          </el-select>
          <el-button style="margin-left: 20px" type="primary" icon="Search" @click="get">搜索</el-button>
          <el-button style="margin-left: 20px" icon="RefreshRight" @click="reset">重置</el-button>
        </div>
        <div v-for="(item, index) in itemData['list']" :key="index" class="item-wrapper"
             @click="openNewTab(item['routeId'])">
          <div class="content-wrapper">
            <!-- 左侧图片 -->
            <img :src="item['pic']" alt="图片" class="main-image">
            <!-- 右侧内容 -->
            <div class="content-container">
              <div class="route-item">
                <div class="route-content">
                  <h2 class="route-title">{{ item['title'] }}</h2>
                  <div class="route-description">{{ item['special'] }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div page>
          <pagination :pageCurrent="itemData?.current" :pageSize="itemData?.size"
                      :small="false" :total="itemData?.total"
                      class="page" @size-change="sizeChange" @current-change="currentChange"/>
        </div>
      </div>
    </div>
    <div style="margin-top: 20px;">
      <div style="width: 98%;margin: auto;height: 1px;background-color: #002B49;"/>
      <div class="copyright">© 2025 旅游网站</div>
    </div>
  </el-scrollbar>
</template>
<style lang="scss" scoped>
[page] {
  padding: 20px 100px;
}

.route-content {
  width: 100%;
}

.container {
  background-image: url('/bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: repeat-x;
}

.search_box {
  width: 100%;
  padding: 0 20px;
  line-height: 60px;
}

/* 每个项目的容器 */
.item-wrapper {
  margin-bottom: 10px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
  cursor: pointer;
}

.item-wrapper:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.content-wrapper {
  display: flex;
  gap: 10px;
}


.main-image {
  width: 250px;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  //margin: 50px 20px 30px 50px;
  margin: auto 0 auto 20px;
}

.content-container {
  width: 60%;
  //margin-top: 60px;
  margin-top: 10px;
}

.route-item {

}

.route-number {
  //font-size: 30px;
  //font-weight: bold;
  color: #037081;
  min-width: 40px;
  line-height: 1.2;
}

.route-title {
  margin: 0 0 10px 20px;
  text-align: left;
  //font-size: 20px;
  color: #333;
  //font-weight: 600;
}

.main-image:hover {
  transform: scale(1.05);
  transition: transform 0.3s linear;
}

.route-title:hover, .route-description:hover {
  transform: scale(1.05);
  text-decoration: underline;
  transition: transform 0.3s linear;
}

.route-description {
  margin-left: 20px;
  letter-spacing: 1px;
  width: 100%; /* 明确设置容器宽度 */
  height: auto; /* 根据内容自动调整高度 */
  word-wrap: break-word; /* 允许长单词换行 */
  overflow-wrap: anywhere; /* 在任意字符间断开（浏览器兼容性更好） */
  white-space: normal; /* 默认值：允许换行 */
}

[con] {
  //background-color: #037081;
  padding-top: 30px;
}

[travel] {
  width: 90%;
  margin: auto;
  font-family: 'Microsoft YaHei', sans-serif;
  background-color: white;
  padding-bottom: 25px;
  border-radius: 6px;

  [tr-top] {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    background-color: #0062A5;
    color: #FFFFFF;
    padding: 25px;

    [dis] {
      display: flex;
      justify-content: space-between;
      font-size: 24px;
      font-weight: bold;
      padding-left: 10px;
      border-left: 4px solid #1890ff;
      align-items: center;

      div:nth-child(2) {
        font-size: 16px;
        font-weight: 400;
      }
    }
  }

  .route-list {
    margin: 30px 0;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    gap: 15px;
  }

  .route-card {
    width: 22%;
    height: 230px;
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background-size: cover;
    background-position: center;
    border-radius: 5px;
  }

  .card-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  }

  .news-list {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
  }

  .news-item {
    display: flex;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #eee;
    cursor: pointer;
  }

  .news-item:last-child {
    border-bottom: none;
  }

  .news-date {
    color: #999;
    font-size: 15px;
    min-width: 90px;
  }

  .news-title {
    color: #333;
    font-size: 20px;
    margin-left: 20px;
  }
}

.copyright {
  color: #002B49;
  font-size: 12px;
  padding: 20px;
  font-family: 'Microsoft YaHei', sans-serif;
}
</style>